
*****************************************************************************************************
*TO RUN THIS DO FILE: Edit Lines 10-11 below to insert your username and the directory you are using*
*****************************************************************************************************

if("`c(username)'"=="") {
cd ""
} 

if("`c(username)'"=="jordan.becker") {
cap cd "C:/Users/jordan.becker/My Drive/1_Research/disagg_milex_demand"
cap cd "G:\My Drive\1_Research\disagg_milex_demand"
}
set more off


********************
* Prepare the Data *
********************
********************************************
*Pull milex data using python scraping file*
********************************************

#delimit;
do "do/eda_nato_data_cleaning";
use "dta/nato_eda_compiled_data", clear;
rename milburden Milburden;
gen countryname=country;
replace countryname = "Macedonia" if countryname == "North Macedonia";
replace countryname = "Bulgaria" if countryname =="Bulgaria ";
replace countryname = "Slovakia" if countryname =="Slovak Republic";
drop if countryname == "Türkiye" & year<2022;
replace countryname = "Turkey" if countryname =="Türkiye";
replace countryname = "Czech Republic" if countryname=="Czechia" & year>=2023;
drop if countryname=="Czechia";
run do/name_to_cow_easy;
list year countryname if ccode==.;
drop if ccode==.;
duplicates list ccode year;

saveold "dta/nato_eda_compiled_data", replace;

*Merge with older milex data*
#delimit;
merge 1:1 ccode year using "dta/milex";
drop _merge;
replace countryname=countryname[_n-1] if countryname=="" & milburden!=.;
replace Milburden=milburden if Milburden==.;
saveold "dta/disagg_milex_demand", replace;

****************************
*Check Paul's Data Concerns*
****************************
***Problem that min of lm is -10 so drop if not>0
***Also use lshc to only consider countries for which we have 
***might want to drop Luxemburg as it is always a problem
#delimit;
use "dta/disagg_milex_demand", clear;
gen lshc = 0;
replace  lshc = 1 if milburden>0 & personnel>0 & equipment>0 & operating>0 & infrastructure>0;
summarize lshc milburden personnel equipment operating infrastructure;
summarize lshc milburden personnel equipment operating infrastructure if lshc ==1 ;
summarize personnel_gdp equipment_gdp operating_gdp infrastructure_gdp ;
summarize personnel_gdp equipment_gdp operating_gdp infrastructure_gdp if lshc ==1 ;
list year countryname milburden personnel equipment operating infrastructure if lshc==0 & [nato_year_dummy==1|eu_year_dummy==1];

*Some of these numbers are abit strange min operating =-1.9 min milburden - 0.000017 and equipment =0*  
*lshc does not reduce the obs as much as expectd*
*The only observations we lose here are Malta 2013 (0 infrastructure) & 2016 (0 equipment), Finland 2018 (0 infrastructure), and Luxembourg 2002 (-1.9 operating). Only this last is wildly odd.*

*Current Covariates: all categories of milex, gdp, population, unemployment, russia spatial, political violence, atlanticism, years nato, nato year dummy, eu year dummy, russia milburden, usa milburden spillins, alldef, nato_alldef, eu_alldef nato_or_eu_alldef, *


**************
*Get WB  Data*
**************
*Get WB GDP data Current USD*
#delimit;
wbopendata, language(en - English) country() topics() indicator(NY.GDP.MKTP.CD) clear long;
do "do/name_to_cow_easy.do";
sort ccode year;
rename countryname wb_countryname;
drop if ccode==.;
gen ln_gdp_current = ln(ny_gdp_mktp_cd);
lab var ln_gdp_current "GDP, Current USD (WB), log";
lab var ny_gdp_mktp_cd "GDP, Current USD (WB)";
saveold "dta/wb_gdp", replace;
merge 1:1 ccode year using "dta/disagg_milex_demand";
drop _merge;
drop if ccode==.;
saveold "dta/disagg_milex_demand", replace;

*Get WB GDP data 2010 USD*
#delimit;
wbopendata, language(en - English) country() topics() indicator(NY.GDP.MKTP.KD) clear long;
do "do/name_to_cow_easy.do";
sort ccode year;
rename countryname wb_countryname;
drop if ccode==.;
saveold "dta/wb_gdp", replace;
merge 1:1 ccode year using "dta/disagg_milex_demand";
drop _merge;
drop if ccode==.;
lab var ny_gdp_mktp_kd "GDP, Constant USD (WB)";
lab var ln_gdp "GDP, Constant USD (WB), log";
saveold "dta/disagg_milex_demand", replace;

*Get WB population data*
#delimit;
wbopendata, language(en - English) country() topics() indicator(SP.POP.TOTL) clear long;
do "do/name_to_cow_easy.do";
sort ccode year;
rename countryname wb_countryname;
drop if ccode==.;
saveold "dta/wb_pop", replace;
merge 1:1 ccode year using "dta/disagg_milex_demand";
drop _merge;
drop if ccode==.;
lab var sp_pop_totl "Population, Total (WB)";
gen ln_pop = ln(sp_pop_totl);
lab var ln_pop "Population, Total (WB), log";
saveold "dta/disagg_milex_demand", replace;


*Get WB UEM data*
#delimit;
wbopendata, language(en - English) country() topics() indicator(SL.UEM.TOTL.ZS - Unemployment, total (% of total labor force) (modeled ILO estimate)) clear long;
do "do/name_to_cow_easy.do";
sort ccode year;
rename countryname wb_countryname;
drop if ccode==.;
saveold "dta/wb_uem", replace;
lab var sl_uem_totl_zs "Unemployment, total (% of total labor force) (modeled ILO estimate)";
merge 1:1 ccode year using "dta/disagg_milex_demand";
drop _merge;
drop if ccode==.;
saveold "dta/disagg_milex_demand", replace;

*Get WB political violence data*
#delimit;
wbopendata, language(en - English) country() topics() indicator(PV.EST) clear long;
do "do/name_to_cow_easy.do";
sort ccode year;
rename countryname wb_countryname;
drop if ccode==.;
saveold "dta/wb_politicalviolence", replace;
merge 1:1 ccode year using "dta/disagg_milex_demand";
lab var pv_est "Political Stability and Absence of Violence/Terrorism: Estimate, WB";
drop _merge;
drop if ccode==.;

saveold "dta/disagg_milex_demand", replace;

*Generate nato_eu_dummy*
#delimit;
use "dta/disagg_milex_demand", clear;
gen nato_eu_dummy=0;
gen nato_dummy=0;
gen eu_dummy=0;
egen nato_dummy_mean=mean(nato_year_dummy), by(ccode);
egen eu_dummy_mean=mean(eu_year_dummy), by(ccode);
replace nato_eu_dummy=1 if nato_dummy_mean>0&eu_dummy_mean>0 & nato_dummy_mean!=. & eu_dummy_mean!=.;
replace nato_dummy=1 if nato_dummy_mean>0 & nato_dummy_mean!=.;
replace eu_dummy=1 if eu_dummy_mean>0 & eu_dummy_mean!=.;
replace nato_dummy=0 if nato_dummy_mean==.;
replace eu_dummy=0 if eu_dummy_mean==.;

*Generate nato_or_eu_dummy*
#delimit;
drop nato_or_eu_dummy;
gen nato_or_eu_dummy=0;
replace nato_or_eu_dummy=1 if nato_dummy_mean>0|eu_dummy_mean>0;
replace nato_or_eu_dummy=0 if nato_dummy_mean==. | eu_dummy_mean==.;

drop nato_dummy_mean;
drop eu_dummy_mean;

replace years_nato=0 if years_nato==.;

saveold "dta/disagg_milex_demand", replace;

*Generate log values if not already present*
#delimit;
use "dta/disagg_milex_demand", clear;
gen ln_milburden=ln(milburden);
lab var ln_milburden "Milex/GDP (ln)";
gen ln_russia_milburden = ln(russia_milburden);
lab var ln_russia_milburden "Milex/GDP, Russia (ln)";
gen ln_russia_spatial = ln(russia_spatial);
lab var ln_russia_spatial "Russia Threat (Russia Milex x Inverse Capital Distance), ln";
gen russia_spatial_milburden=russia_milburden*1/(capdist_russia);
lab var russia_spatial_milburden "Russia Threat (Russia Milburden x Inverse Capital Distance)";
gen ln_russia_spatial_milburden=ln(russia_spatial_milburden);
lab var ln_russia_spatial_milburden "Russia Threat (Russia Milburden x Inverse Capital Distance), ln";
gen ln_usa_milburden = ln(usa_milburden);
lab var ln_usa_milburden "Milex/GDP, USA (ln)";
gen ln_ny_gdp_mktp_kd = ln(ny_gdp_mktp_kd);
lab var ln_ny_gdp_mktp_kd "GDP, Constant USD (WB), ln";
gen ln_personnel_gdp = ln(personnel_gdp);
lab var ln_personnel_gdp "Personnel/GDP, ln";
gen ln_equipment_gdp = ln(equipment_gdp);
lab var ln_equipment_gdp "Equipment/GDP, ln";
gen ln_operating_gdp = ln(operating_gdp);
lab var ln_operating_gdp "O&M/GDP, ln";
gen ln_infrastructure_gdp = ln(infrastructure_gdp);
lab var ln_infrastructure_gdp "Infrastructure/GDP, ln";
gen ln_equipment = ln(equipment);
lab var ln_equipment "Equipment/Milex, ln";
gen ln_personnel = ln(personnel);
lab var ln_personnel "Personnel/Milex, ln";
gen ln_operating = ln(operating);
lab var ln_operating "O&M/Milex, ln";
gen ln_infrastructure = ln(infrastructure);
lab var ln_infrastructure "Infrastructure/GDP, ln";

gen ln_sl_uem_totl_zs = ln(sl_uem_totl_zs);
lab var ln_sl_uem_totl_zs "Unemployment Rate, ln";
gen ln_pv_est = ln(pv_est);
lab var ln_pv_est "Political Stability/Violence, WB, ln";
gen ln_atlanticism = ln(atlanticism);
lab var ln_atlanticism "Atlanticism, ln";
gen ln_years_nato = ln(years_nato);
lab var ln_years_nato "Years in NATO, ln";
replace ln_years_nato = 0 if ln_years_nato==.;
gen years_nato_positive = years_nato if years_nato>0;






xtset ccode year;
drop if countryname=="United States of America";
saveold "dta/disagg_milex_demand", replace;



****************
*BALANCE PANELS*
****************
#delimit;
use "dta/disagg_milex_demand", clear;
keep countryname ccode year ln_milburden ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins nato_or_eu_dummy ln_personnel_gdp ln_equipment_gdp ln_operating_gdp ln_infrastructure_gdp milburden equipment personnel infrastructure operating equipment_gdp personnel_gdp infrastructure_gdp operating_gdp ny_gdp_mktp_kd sl_uem_totl_zs sp_pop_totl russia_spatial pv_est atlanticism years_nato years_nato_positive spatial_spillins usa_milburden russia_milburden ln_russia_milburden ln_usa_milburden nato_dummy ln_gdp;
drop if nato_or_eu_dummy!=1;
spbalance, balance;
tabstat ln_milburden ln_equipment, by(ccode);
tabstat ln_milburden ln_equipment, by(countryname);
tabstat year, by(countryname) stats(min max);
list year countryname ln_milburden ln_personnel_gdp;
save "dta/disagg_milex_demand_balanced", replace;
tabstat year, by(countryname) stats(min max);
********************
* Analyze the Data *
********************

************************
*Table 1: summary Stats*
************************
#delimit;
use "dta/disagg_milex_demand", clear;
xtset ccode year;
fsum milburden equipment personnel infrastructure operating ny_gdp_mktp_kd sl_uem_totl_zs sp_pop_totl russia_spatial pv_est atlanticism years_nato years_nato_positive spatial_spillins usa_milburden year if nato_or_eu_dummy==1 & milburden!=., label;

*********************
*Table 2A: crosstabs*
*********************
*Combine these two!*
#delimit;
tabstat milburden equipment personnel infrastructure operating if nato_or_eu_dummy==1, stats(mean) by(countryname);
reg milburden equipment personnel infrastructure operating
tabstat year if e(sample)==1, by(countryname) stats(min max)

******************************************
*Table 2: lagged dv, ln_gdp, russia, usa*
******************************************
#delimit;
use "dta/disagg_milex_demand", clear;
xtreg ln_milburden ln_ny_gdp_mktp_kd ln_russia_milburden ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table2.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) replace;
xtreg ln_milburden l.ln_milburden ln_ny_gdp_mktp_kd ln_russia_milburden ln_usa_milburden if nato_or_eu_dummy==1, fe; 
outreg2 using "output/table2.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

*log linear share of GDP
#delimit;
xtreg ln_personnel_gdp l.ln_personnel_gdp ln_ny_gdp_mktp_kd  ln_russia_milburden ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table2.xls", ctit(Personnel/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_equipment_gdp l.ln_equipment_gdp ln_ny_gdp_mktp_kd  ln_russia_milburden ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table2.xls", ctit(Equipment/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_operating_gdp l.ln_operating_gdp ln_ny_gdp_mktp_kd  ln_russia_milburden ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table2.xls", ctit(O&M/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_infrastructure_gdp l.ln_infrastructure_gdp ln_ny_gdp_mktp_kd  ln_russia_milburden ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table2.xls", ctit(Infrastructure/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

*************************************
*Table 3: Extended Model, log linear*
*************************************

#delimit;
use "dta/disagg_milex_demand", clear;
xtreg ln_milburden ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table3.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) replace;

xtreg ln_milburden l.ln_milburden ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe; 
outreg2 using "output/table3.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

test ln_atlanticism ln_pv_est;


*log linear share of GDP
#delimit;
xtreg ln_personnel_gdp l.ln_personnel_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table3.xls", ctit(Personnel/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

test ln_atlanticism ln_pv_est;

xtreg ln_equipment_gdp l.ln_equipment_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table3.xls", ctit(Equipment/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

test ln_atlanticism ln_pv_est;

xtreg ln_operating_gdp l.ln_operating_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table3.xls", ctit(O&M/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

test ln_atlanticism ln_pv_est;

xtreg ln_infrastructure_gdp l.ln_infrastructure_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table3.xls", ctit(Infrastructure/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

test ln_atlanticism ln_pv_est;

*Now replicate without atlancism and pv_est*

#delimit;
use "dta/disagg_milex_demand", clear;
xtreg ln_milburden ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial  ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table3a.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) replace;

xtreg ln_milburden l.ln_milburden ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial  ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe; 
outreg2 using "output/table3a.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;




*log linear share of GDP
#delimit;
xtreg ln_personnel_gdp l.ln_personnel_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial  ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table3a.xls", ctit(Personnel/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;



xtreg ln_equipment_gdp l.ln_equipment_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial  ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table3a.xls", ctit(Equipment/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;



xtreg ln_operating_gdp l.ln_operating_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial  ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table3a.xls", ctit(O&M/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;



xtreg ln_infrastructure_gdp l.ln_infrastructure_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial  ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table3a.xls", ctit(Infrastructure/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;




******************************************
*Table 4: Disaggregated as Share of Milex*
******************************************

#delimit;
use "dta/disagg_milex_demand", clear;
xtreg ln_milburden ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table4.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) replace;

xtreg ln_milburden l.ln_milburden ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe; 
outreg2 using "output/table4.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

#delimit;
xtreg ln_personnel l.ln_personnel ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table4.xls", ctit(Personnel/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_equipment l.ln_equipment ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table4.xls", ctit(Equipment/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_operating l.ln_operating ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table4.xls", ctit(O&M/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_infrastructure l.ln_infrastructure ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table4.xls", ctit(Infrastructure/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;


*Log linear share m with mil burden 
#delimit;
xtreg ln_personnel l.ln_personnel ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table4.xls", ctit(Personnel/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_equipment l.ln_equipment ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table4.xls", ctit(Equipment/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_operating l.ln_operating ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table4.xls", ctit(O&M/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg ln_infrastructure l.ln_infrastructure ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_milburden if nato_or_eu_dummy==1, fe;
outreg2 using "output/table4.xls", ctit(Infrastructure/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;


***********************************
*Table 5 Dynamic ECM (share milex)*
***********************************
#delimit;
use "dta/disagg_milex_demand", clear;
xtreg d.ln_milburden l.ln_milburden d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table5.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) replace;

xtreg d.ln_personnel l.ln_personnel d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table5.xls", ctit(Personnel/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg d.ln_equipment l.ln_equipment d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table5.xls", ctit(Equipment/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg d.ln_operating l.ln_operating d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table5.xls", ctit(O&M/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg d.ln_infrastructure l.ln_infrastructure d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table5.xls", ctit(Infrastructure/Milex) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;


*********************************
*Table 6 Dynamic ECM (share gdp)*
*********************************

#delimit;
use "dta/disagg_milex_demand", clear;
xtreg d.ln_milburden l.ln_milburden d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table6.xls", ctit(Milex/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) replace;

xtreg d.ln_personnel_gdp l.ln_personnel_gdp d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table6.xls", ctit(Personnel/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg d.ln_equipment_gdp l.ln_equipment_gdp d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table6.xls", ctit(Equipment/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg d.ln_operating_gdp l.ln_operating_gdp d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table6.xls", ctit(O&M/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

xtreg d.ln_infrastructure_gdp l.ln_infrastructure_gdp d.ln_ny_gdp_mktp_kd l.ln_ny_gdp_mktp_kd d.ln_sl_uem_totl_zs l.ln_sl_uem_totl_zs d.ln_pop l.ln_pop d.ln_russia_spatial l.ln_russia_spatial d.ln_pv_est l.ln_pv_est d.ln_atlanticism l.ln_atlanticism d.ln_years_nato l.ln_years_nato d.ln_usa_milburden l.ln_usa_milburden if nato_or_eu_dummy==1, fe;   
outreg2 using "output/table6.xls", ctit(Infrastructure/GDP) label addtext (Fixed Effects, country) bdec(3) cdec(3) pdec(3) tdec(3) rdec(3) append;

************************
*Table 7: Arellano-Bond*
************************

#delimit;
use "dta/disagg_milex_demand", clear;

xtabond ln_milburden ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(Milex/GDP) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) replace;

xtabond ln_personnel_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(Personnel/GDP) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) append;

xtabond ln_equipment_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(Equipment/GDP) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) append;

xtabond ln_operating_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(O&M/GDP) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) append;

xtabond ln_infrastructure_gdp ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(Infrastructure/GDP) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) append;

xtabond ln_personnel ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(Personnel/Milex) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) append;

xtabond ln_equipment ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(Equipment/Milex) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) append;

xtabond ln_operating ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(O&M/Milex) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) append;

xtabond ln_infrastructure ln_ny_gdp_mktp_kd ln_sl_uem_totl_zs ln_pop ln_russia_spatial ln_pv_est ln_atlanticism ln_years_nato ln_usa_milburden ln_spatial_spillins if nato_or_eu_dummy==1, maxldep(1) maxlags(2) vce(robust);
outreg2 using "output/table7xtabond.xls", ctit(Infrastructure/Milex) label addtext() bdec(4) cdec(4) pdec(4) tdec(4) rdec(4) append;

***************
*Table 8: ARDL*
***************

***********************************************************************
*Figure 1: Disaggregated Bars for countries with data from 1975 & 2023*
***********************************************************************
#delimit;
set scheme gg_tableau;
use "dta/disagg_milex_demand", clear;

tabstat year if equipment_gdp!=. & personnel_gdp!=. & operating_gdp!=. & infrastructure_gdp!=., statistics (min max) by(countryname);
sort ccode year;
gen disagg1975=0;
replace disagg1975=1 if countryname=="Belgium"|countryname=="Canada"|countryname=="Denmark"|countryname=="Germany"|countryname=="Italy"|countryname=="Luxembourg"|countryname=="Netherlands"|countryname=="Norway"|countryname=="Portugal"|countryname=="United Kingdom";


preserve;
graph bar (mean) equipment_gdp (mean) personnel_gdp (mean) operating_gdp (mean) infrastructure_gdp if [year==1975|year==2023] & disagg1975==1, scheme (economist) 
over (year, label(labcolor(red) angle(ninety) labsize(tiny))) 
over(countryname, label(labcolor(black) 
angle(forty_five) labsize(vsmall))) stack yline(2 .4, lcolor(red) lpattern(dash) lwidth(medthick)) 
ytitle("Expenditures/GDP (%), by category (1975 & 2023)", size(small) margin(medium)) legend(rows(1) position(6)  size(vsmall) 
label (1 "equipment") label (2 "personnel") label (3 "O&M")  label (4 "infrastructure")) ysize (10) ylab(0(.5)6.5, labsize(vsmall));
graph save output\Figure1disagg_1975.gph, replace;
graph export figures\Figure1disagg_1975.tif, replace;
restore;

***************************************
*Figure 2: Disaggregated Bars for 2021*
***************************************
#delimit;
set scheme gg_tableau;
use "dta/disagg_milex_demand", clear;

tabstat year if equipment_gdp!=. & personnel_gdp!=. & operating_gdp!=. & infrastructure_gdp!=., statistics (min max) by(countryname);
sort ccode year;

preserve;
graph bar (mean) equipment_gdp (mean) personnel_gdp (mean) operating_gdp (mean) infrastructure_gdp if [year==2014|year==2023] & nato_dummy==1 & equipment!=., scheme () 
over (year, label(labcolor(red) angle(ninety) labsize(tiny))) 
over(countryname, label(labcolor(black) 
angle(forty_five) labsize(vsmall))) stack yline(2 .4, lcolor(red) lpattern(dash) lwidth(medthick)) 
ytitle("Expenditures/GDP (%), by category (2014 & 2023)", size(small) margin(medium)) legend(rows(1) position(6)  size(vsmall) 
label (1 "equipment") label (2 "personnel") label (3 "O&M")  label (4 "infrastructure")) ysize (10) ylab(0(.25)4, labsize(vsmall));
graph save output\Figure2disagg_pledge.gph, replace;
graph export figures\Figure2disagg_pledge.tif, replace;
restore;

****************************************
*Figure 3: Disaggregated Bars all Years*
****************************************

#delimit;
set scheme gg_tableau;
use "dta/disagg_milex_demand", clear;

tabstat year if equipment_gdp!=. & personnel_gdp!=. & operating_gdp!=. & infrastructure_gdp!=., statistics (min max) by(countryname);
sort ccode year;

preserve;
drop if countryname=="Iceland"|countryname=="United States";
graph bar (mean) equipment_gdp (mean) personnel_gdp (mean) operating_gdp (mean) infrastructure_gdp if year>=1985 & year<=2023, 
 
over(year, label(labcolor(black) 
angle(forty_five) labsize(vsmall))) stack yline(2 .4, lcolor(red) lpattern(dash) lwidth(medthick)) 
ytitle("Expenditures/GDP (%), by category", size(small) margin(medium)) legend(rows(1) position(6)  size(vsmall) 
label (1 "equipment") label (2 "personnel") label (3 "O&M")  label (4 "infrastructure")) ysize (10) ylab(0(.5)3.5, labsize(vsmall));
graph save output\Figure3disagg_all_years.gph, replace;
graph export figures\Figure3disagg_all_years.tif, replace;
restore;

*******************************************************
*Figure 4: Scatterfit By Category - Olson & Zeckhauser*
*******************************************************

*Figure TBD - scatterfit by year*

#delimit;
use "dta/disagg_milex_demand", clear;

scatterfit ln_milburden ln_gdp if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls() fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\publicchoice_milburden.gph, replace;

scatterfit ln_personnel_gdp ln_gdp if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls() fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\publicchoice_personnel.gph, replace;

scatterfit ln_equipment_gdp ln_gdp if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls() fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\publicchoice_equipment.gph, replace;

scatterfit ln_operating ln_gdp if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls() fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\publicchoice_operating.gph, replace;

scatterfit ln_infrastructure ln_gdp if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls() fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\publicchoice_infrastructure.gph, replace;


grc1leg  output\publicchoice_milburden.gph output\publicchoice_personnel.gph output\publicchoice_equipment.gph output\publicchoice_operating.gph output\publicchoice_infrastructure.gph, 
title("", size(medlarge) margin(medsmall) color(black) position(6))
subtitle("", size(medarge) position(9) orientation(vertical) margin(medsmall) color(black));
graph save output\publicchoice_bytype, replace;
graph export figures\publicchoice_bytype.tif, replace;

*******************************************************
*Figure 5: Scatterfit By Category - Dunne & Smith*
*******************************************************

#delimit;
use "dta/disagg_milex_demand", clear;

gen l1_ln_milburden = ln_milburden[_n-1];
gen l1_ln_personnel = ln_personnel[_n-1];
gen l1_ln_equipment = ln_equipment[_n-1];
gen l1_ln_operating = ln_operating[_n-1];
gen l1_ln_infrastructure = ln_infrastructure[_n-1];

scatterfit ln_milburden ln_ny_gdp_mktp_kd if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls(l1_ln_milburden ln_russia_milburden ln_usa_milburden) fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\dunnesmith_milburden.gph, replace;

scatterfit ln_personnel ln_ny_gdp_mktp_kd if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls(l1_ln_personnel ln_russia_milburden ln_usa_milburden) fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\dunnesmith_personnel.gph, replace;

scatterfit ln_equipment ln_ny_gdp_mktp_kd if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls(l1_ln_equipment ln_russia_milburden ln_usa_milburden) fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\dunnesmith_equipment.gph, replace;

scatterfit ln_operating ln_ny_gdp_mktp_kd if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls(l1_ln_operating ln_russia_milburden ln_usa_milburden) fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\dunnesmith_operating.gph, replace;

scatterfit ln_infrastructure ln_ny_gdp_mktp_kd if nato_or_eu_dummy==1 & equipment!=. & personnel!=. & operating!=. & infrastructure!=., mlab() fit(lfitci) controls(l1_ln_infrastructure ln_russia_milburden ln_usa_milburden) fcontrols() regparameters(coef sig pval r2 nobs)  scale(.7);
graph save output\dunnesmith_infrastructure.gph, replace;


grc1leg  output\dunnesmith_milburden.gph output\dunnesmith_personnel.gph output\dunnesmith_equipment.gph output\dunnesmith_operating.gph output\dunnesmith_infrastructure.gph, 
title("", size(medlarge) margin(medsmall) color(black) position(6))
subtitle("", size(medarge) position(9) orientation(vertical) margin(medsmall) color(black));
graph save output\dunnesmith_bytype, replace;
graph export figures\dunnesmith_bytype.tif, replace;


